package com.luoran.system.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.EasyExcel;
import com.luoran.system.domain.Examples;
import com.luoran.system.domain.UserTest;
import com.luoran.system.service.TradesmanCityService;
import com.luoran.system.service.UserTestService;
import com.luoran.system.utils.Result;
import io.minio.MinioClient;

import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@RestController
@RequestMapping("userTest")
public class UserTestController {

    public static final String TEST = "test";
    @Autowired
    UserTestService userTestService;

    @Autowired
    TradesmanCityService tradesmanCityService;//三级联动数据

    @RequestMapping("getExcel")//新建导出
    public void getExcel(HttpServletResponse response) throws IOException {
        List<UserTest> list = userTestService.selectUserTest();
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        // 这里URLEstring a="an;coder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("用户导出", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), UserTest.class).sheet("seet页1").doWrite(list);
    }

    /**
     * 查看导出有没有数据
     * @return
     */
    @RequestMapping("listUserTest")
    public Result listUserTest(){
        List<UserTest> list = userTestService.selectUserTest();
        return Result.success(list);
    }

    /**
     * 年龄统计图数据
     * @return
     */
    @RequestMapping("barChart")
    public Result barChart(){
        List<Examples> list = userTestService.selecExamples();
        return Result.success(list);
    }

    /**
     * 部门统计图数据
     * @return
     */
    @RequestMapping("deptList")
    public Result deptList(){
        List<Examples> list = userTestService.selecDeptList();
        return Result.success(list);
    }


    //上传图片
//    @RequestMapping("uploadFile")
//    public String uploadFile(MultipartFile file) throws InvalidPortException, InvalidEndpointException, IOException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, InvalidPortException, InvalidEndpointException, InvalidBucketNameException, InsufficientDataException, NoSuchAlgorithmException, InvalidKeyException {
//        //改写名字
//        String filename = file.getOriginalFilename();//获取名字
//        filename = filename.substring(filename.lastIndexOf("."));
//        filename = IdUtil.getSnowflakeNextIdStr()+filename;
//        //配置minio密钥
//        MinioClient minioClient = new MinioClient("http://localhost:9000", "mzFfrfXXtp6DZQw02Z4m", "BQYa0SDyv8nE1hFtX0GMqyr4FSbOOpCOGQwraoDu");
//
//        PutObjectOptions objectOptions = new PutObjectOptions(file.getSize(), -1);
//        objectOptions.setContentType(file.getContentType());
//        minioClient.putObject(TEST,filename,file.getInputStream(),objectOptions);
//
//        String url = "http://localhost:9000/"+TEST+"/"+filename;
//
//        return url;
//    }

    /**
     * 三级联动数据
     * @return
     */
    @RequestMapping("dataCity")
    public List<Tree<Object>> dataCity(){
        return tradesmanCityService.selectDataCity();
    }



}
